package com.mxb.service.impl;

import com.mxb.annotation.Autowired;
import com.mxb.annotation.Service;
import com.mxb.annotation.Transactional;
import com.mxb.dao.AccountDao;
import com.mxb.pojo.Account;
import com.mxb.service.TransferService;

/**
 * Service层
 *
 * @Author: MaXiaobing
 * @Date: 2021/4/17 12:34
 */
@Service("transferService")
@Transactional
public class TransferServiceImpl implements TransferService {

    @Autowired
    private AccountDao accountDao;

    public void setAccountDao(AccountDao accountDao) {
        this.accountDao = accountDao;
    }

    @Override
    public void transfer(String fromCardNo, String toCardNo, int money) throws Exception {
        /*try {
            // 开启事务(关闭事务的自动提交)
            TransactionManager.getInstance().beginTransaction();*/

            Account from = accountDao.queryAccountByCardNo(fromCardNo);
            Account to = accountDao.queryAccountByCardNo(toCardNo);

            from.setMoney(from.getMoney() - money);
            to.setMoney(to.getMoney() + money);

            accountDao.updateAccountByCardNo(to);
            int c = 1 / 0;
            accountDao.updateAccountByCardNo(from);

            /*// 提交事务
            TransactionManager.getInstance().commit();
        } catch (Exception e) {
            e.printStackTrace();
            // 回滚事务
            TransactionManager.getInstance().rollback();

            // 抛出异常便于上层servlet捕获
            throw e;
        }*/
    }

}

